<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title">
        <div class="page-title-con"><span>税费计算器</span><div class="move_title"></div>
            <div class="page-title-opt-list">
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" onclick="print_neirong()">打印</button>
                <button type="button" class="layui-btn layui-btn-sm btn-dark marginr5" lay-submit="" lay-filter="add">开始计算</button>
                <span id="page-close" class="page-close" onclick="layer_close()"><i class="layui-icon layui-icon-close"></i></span>
            </div>
        </div>
    </div>
    <div class="main-container" id="yinneirong">

        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=shuisuan','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=shuisuan','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="shihemoney" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同税率(%)</label>
                        <div class="layui-input-block">
                            <input type="text" id="shirate" readonly placeholder="请输入" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">应缴成本票比例(%)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piaorate" readonly placeholder="请输入" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">成本票差额费比例(%)</label>
                        <div class="layui-input-block">
                            <input type="text" id="charate" readonly placeholder="请输入" autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">已开票金额含税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piao_kai_money" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">销项税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piao_kai_ratefee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">进项票金额含税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piao_shou_money" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">进项税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="piao_shou_ratefee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>本次拟开票</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>本次拟开票金额含税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="benkai" name="benkai" value="0" placeholder="请输入" clearable autocomplete="off" class="layui-input moneyinput">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>本次拟开票税率(%)</label>
                        <div class="layui-input-block">
                            <input type="text" id="benrate" name="benrate" value="0" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">本次拟开票税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="benratefee" readonly placeholder="自动计算：本次拟开票金额含税÷（1+本次拟开票税率）X本次拟开票税率" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>本次拟收成本票金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="benshou" name="benshou" value="0" placeholder="请输入" clearable autocomplete="off" class="layui-input moneyinput">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">本次拟收成本票税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="benshouratefee" value="0" placeholder="请输入" clearable autocomplete="off" class="layui-input moneyinput">
                        </div>
                    </div>
                </div>
            </div>



            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>截至本次票税情况</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开票金额含税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="zong_piao_kai_money" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">销项税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="zong_piao_kai_ratefee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">进项票金额含税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="zong_piao_shou_money" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">进项税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="zong_piao_shou_ratefee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">增值税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="zengzhishui" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>计税规则</span></div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>税种</th>
                            <th>计算项</th>
                            <th>计算值</th>
                            <th>计算说明</th>
                            <th>应计税费金额(元)</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list" style="max-height:400px"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>


        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>税费计算结果</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">应收取税费合计(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="yingshou_shuifee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">已扣收税费合计(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="yikou_shuifee" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">本次拟开票应计税费(元)</label>
                        <div class="layui-input-block">
                            <input type="text" id="benni_yingshui" readonly placeholder="请输入" autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</form>
<script>

    $("#commentForm").validate({
        rules: {
            benkai: {required: true},
            benrate: {required: true},
            benshou: {required: true},
        },
        messages:{
            benkai: {required: "请输入"},
            benrate: {required: "请输入"},
            benshou: {required: "请输入"},
        }
    });


    var pub_xiang, pub_name;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        facade.ajax({url: ask_engin_shuisuan,data:{id:0}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;

                pub_name = res.data.name;
                pub_xiang = res.data.xiang;



                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px" class="listnums">'+nums+'</td>';
                        str += '<td><input type="text" class="layui-input name disabled" disabled key="'+list[key].name+'" value="'+pub_name[list[key].name]+'"></td>';
                        str += '<td><select class="xiang" lay-filter="xiang">';
                        str += '<option value="" selected>请选择</option>';
                        for (var mm in pub_xiang){
                            if(mm == list[key].xiang){
                                str += '<option value="'+mm+'" selected>'+pub_xiang[mm]+'</option>';
                            }else{
                                str += '<option value="'+mm+'">'+pub_xiang[mm]+'</option>';
                            }
                        }
                        str += '</select></td>';

                        str += '<td><input type="number" class="layui-input rateval" value="'+list[key].rateval+'"></td>';
                        str += '<td><input type="text" class="layui-input explain disabled" value="'+set_explain(list[key].xiang,list[key].rateval)+'" disabled></td>';
                        str += '<td><input type="number" class="layui-input yingshui disabled" disabled></td>';
                        str += '</tr>';
                    }

                    str += '<tr>';
                    str += '<td style="width: 45px" class="listnums disabled" disabled>合计</td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td></td>';
                    str += '<td><input type="number" class="layui-input heji_yingshui disabled" disabled></td>';
                    str += '</tr>';

                    $("#list_list").html(str);
                }
                update_refresh_row_func()



                initParseMoney();
                render_from();
            }
        });
    });



    function get_engin_info(enginid){
        $("#list_list").html('');
        $("#yingshou_shuifee").val('');
        $("#yikou_shuifee").val('');
        $("#benni_yingshui").val('');
        $("#benkai").val('');
        $("#zong_piao_kai_money").val('');
        $("#zong_piao_kai_ratefee").val('');
        $("#zong_piao_shou_money").val('');
        $("#zong_piao_shou_ratefee").val('');
        $("#zengzhishui").val('');



        layui.use(['laytp', 'form'], function() {
            var form = layui.form, layer = layui.layer;
            facade.ajax({url: ask_engin_shuisuan,data:{id:enginid}}).done(function(res){
                if (res.code === 2000) {
                    var data = res.data;

                    $("#shihemoney").val(data.shihemoney);
                    $("#shirate").val(data.shirate);
                    $("#benrate").val(data.shirate);
                    $("#piaorate").val(data.piaorate);
                    $("#charate").val(data.charate);
                    $("#piao_kai_money").val(data.piao_kai_money);
                    $("#piao_kai_ratefee").val(data.piao_kai_ratefee);
                    $("#piao_shou_money").val(data.piao_shou_money);
                    $("#piao_shou_ratefee").val(data.piao_shou_ratefee);
                    $("#yikou_shuifee").val(data.yikou_shuifee);


                    var list = res.data.list;
                    if(list.length > 0){
                        var str = '';
                        for (var key in list){
                            var nums = parseInt(key)+1;
                            str += '<tr>';
                            str += '<td style="width: 45px" class="listnums">'+nums+'</td>';
                            str += '<td><input type="text" class="layui-input name disabled" disabled key="'+list[key].name+'" value="'+pub_name[list[key].name]+'"></td>';
                            str += '<td><select class="xiang" lay-filter="xiang">';
                            str += '<option value="" selected>请选择</option>';
                            for (var mm in pub_xiang){
                                if(mm == list[key].xiang){
                                    str += '<option value="'+mm+'" selected>'+pub_xiang[mm]+'</option>';
                                }else{
                                    str += '<option value="'+mm+'">'+pub_xiang[mm]+'</option>';
                                }
                            }
                            str += '</select></td>';

                            str += '<td><input type="number" class="layui-input rateval" value="'+list[key].rateval+'"></td>';
                            str += '<td><input type="text" class="layui-input explain disabled" value="'+set_explain(list[key].xiang,list[key].rateval)+'" disabled></td>';
                            str += '<td><input type="number" class="layui-input yingshui disabled" disabled></td>';
                            str += '</tr>';
                        }

                        str += '<tr>';
                        str += '<td style="width: 45px" class="listnums disabled" disabled>合计</td>';
                        str += '<td></td>';
                        str += '<td></td>';
                        str += '<td></td>';
                        str += '<td></td>';
                        str += '<td><input type="number" class="layui-input heji_yingshui disabled" disabled></td>';
                        str += '</tr>';

                        $("#list_list").html(str);
                    }
                    update_refresh_row_func()



                    initParseMoney();
                    render_from();
                }
            });
        });
    }


    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();


        form.on('select(xiang)', function(data){
            var rateval = $(this).parents('tr').find('.rateval').val();
            $(this).parents('tr').find('.explain').val(set_explain(data.value, rateval));
        });

        $('#list_list').on('input propertychange','.rateval',function () {
            var xiang = $(this).parents('tr').find('.xiang').val();
            var rateval = $(this).parents('tr').find('.rateval').val();
            $(this).parents('tr').find('.explain').val(set_explain(xiang, rateval));
        });


        form.on('submit(add)', function(data){
            data.field.enginid = get_choose_val('enginid');
            var validRes = $("#commentForm").valid();
            // formValid('enginid');
            // if(data.field.enginid == undefined || !data.field.enginid){
            //     validRes = false;
            // }
            if(!validRes){return false;}

            // var myMsg = layer.msg("数据计算中，请稍候...",{
            //     icon:16,
            //     time:-1
            // });


            var zengzhishui = parseValidNum($("#zengzhishui").val()) || 0;
            var zong_piao_kai_money = parseValidNum($("#zong_piao_kai_money").val()) || 0;
            var zong_piao_kai_no = parseFloat(zong_piao_kai_money-zong_piao_kai_ratefee);
            var piao_shou_money = parseValidNum($("#piao_shou_money").val()) || 0;
            var yikou_shuifee = parseValidNum($("#yikou_shuifee").val()) || 0;

            var heji_yingshui = 0;
            var heji_yingshoushui = 0;
            $('#list_list tr').each(function (i) {
                var listnums = $(this).find('.listnums').text() || '';
                var xiang = $(this).find('.xiang').val();
                var rateval = $(this).find('.rateval').val();

                if(listnums != '合计'){
                    var yingshui = 0;

                    if(xiang == 1){
                        yingshui = zengzhishui * rateval;
                    }
                    if(xiang == 2){
                        yingshui = zong_piao_kai_money * rateval;
                    }
                    if(xiang == 3){
                        yingshui = zong_piao_kai_no * rateval;
                    }
                    if(xiang == 4){
                        yingshui = piao_shou_money * rateval;
                    }

                    heji_yingshui = parseFloat(heji_yingshui) + parseFloat(yingshui);

                    $(this).find('.yingshui').val(parseValidNum(yingshui));
                }
            });

            $('.heji_yingshui').val(parseValidNum(heji_yingshui));




            var yingshou_shuifee = parseFloat(zengzhishui) + parseFloat(heji_yingshui);
            var benni_yingshui = parseFloat(yingshou_shuifee) - parseFloat(yikou_shuifee);

            $('#yingshou_shuifee').val(parseFormatNum(yingshou_shuifee));
            $('#benni_yingshui').val(parseFormatNum(benni_yingshui));



            // setTimeout(function () {
            //     layer.close(myMsg);
            //     openPage('suan_result.php');
            // },1000);


            openPage('suan_result.php','60%','60%');
            return false;
        });

    });




    $("#benkai, #benrate, #benshou, #benshouratefee").on('input propertychange',function () {
        var benkai = parseValidNum($("#benkai").val()) || 0;
        var benrate = parseValidNum($("#benrate").val()) || 0;

        var piao_kai_money = parseValidNum($("#piao_kai_money").val()) || 0;
        var piao_kai_ratefee = parseValidNum($("#piao_kai_ratefee").val()) || 0;
        var piao_shou_money = parseValidNum($("#piao_shou_money").val()) || 0;
        var piao_shou_ratefee = parseValidNum($("#piao_shou_ratefee").val()) || 0;
        var benshou = parseValidNum($("#benshou").val()) || 0;
        var benshouratefee = parseValidNum($("#benshouratefee").val()) || 0;

        var benratefee = parseFloat(benkai) / (1+parseFloat(benrate)/100) * (parseFloat(benrate)/100);
        var zong_piao_kai_money = parseFloat(piao_kai_money) + parseFloat(benkai);
        var zong_piao_kai_ratefee = parseFloat(piao_kai_ratefee) + parseFloat(benratefee);
        var zong_piao_shou_money = parseFloat(piao_shou_money) + parseFloat(benshou);
        var zong_piao_shou_ratefee = parseFloat(piao_shou_ratefee) + parseFloat(benshouratefee);
        var zengzhishui = parseFloat(zong_piao_kai_ratefee) - parseFloat(zong_piao_shou_ratefee);

        $("#benratefee").val(parseFormatNum(benratefee,2));
        $("#zong_piao_kai_money").val(parseFormatNum(zong_piao_kai_money,2));
        $("#zong_piao_kai_ratefee").val(parseFormatNum(zong_piao_kai_ratefee,2));
        $("#zong_piao_shou_money").val(parseFormatNum(zong_piao_shou_money,2));
        $("#zong_piao_shou_ratefee").val(parseFormatNum(zong_piao_shou_ratefee,2));
        $("#zengzhishui").val(parseFormatNum(zengzhishui,2));
    });

    // function update_refresh_row_func() {
    //     render_date();
    //     render_from();
    //     $.each($("#list_list").children('tr'),function (key,val) {
    //         $(this).children('td:first-child').html(key+1);
    //     });
    //     setTimeout(function () {
    //         $(".short_table_list table tbody tr").click(function () {
    //             $(".short_table_list table tbody tr").removeClass('line-row');
    //             $(".short_table_list table tbody tr").attr('sle','');
    //             $(this).addClass('line-row');
    //             $(this).attr('sle','sle');
    //         });
    //     },1000);
    // }

    function set_explain(xiangName='', rateval='') {
        if(!xiangName || !rateval || rateval == 0){
            return 0;
        }
        return xiangName+" X "+rateval;
    }











    function set_explain(xiang=0, rateval='') {
        var xiangName = pub_xiang[xiang] || '';
        if(!xiangName || !rateval || rateval == 0){
            return 0;
        }
        return xiangName+" X "+rateval;
    }

    function print_neirong() {
        var enginid = get_choose_val('enginid');
        openPage('suan_print.php?enginid='+enginid);
    }
</script>
</body>
</html>
